package bookshop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bookshop.dao.CustomerDao;
import bookshop.dao.DaoFactory;
import bookshop.dao.ManagerDao;
import bookshop.dao.OrderTraceDao;
import bookshop.domain.OrderTrace;
import bookshop.exception.DataAccessException;
import bookshop.service.helper.OrderTraceQueryHelper;
import bookshop.utils.DBUtils;

public class OrderTraceDaoJDBCImpl implements OrderTraceDao {
	
	private static final String ADD_ORDER_TRACE = "insert into order_trace(order_id,order_status,login_id,cust_id,oper_time) values(?,?,?,?,?)";
    private String generateSQL(OrderTraceQueryHelper helper) {
		
		String sql = "select * from order_trace where 1=1 ";
		
		if(helper.getQryOrderId()!=null)
			sql+=" and order_id like '%"+helper.getQryOrderId()+"%' ";
		
		if(helper.getQryCustId()!=null)
			sql+=" and login_id = '"+helper.getQryCustId()+"' ";
		
		
				sql += " order by id";
			
				return sql;
		}
    public Long countOrderTraceByConditon(OrderTraceQueryHelper helper) {
		String sql =this.generateSQL(helper);
		
		sql =sql.replace("*", "count(*) as total");
		sql =sql.substring(0,sql.indexOf("order by"));
		
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Long count = 0L;
		
		try {
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				count=rs.getLong("total");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().releaseRes(ct, ps, rs);
		}
		
		return count;
	}
	
	public List<OrderTrace> GetOrderTraceByConnection(OrderTraceQueryHelper helper, int startIndex,int fetchSize){
		String sql = this.generateSQL(helper);
		sql += " limit " + startIndex + "," + fetchSize;

		
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;

		List<OrderTrace> orderTraces = new ArrayList<OrderTrace>();

		try {
			ps = ct.prepareStatement(sql);
			rs = ps.executeQuery();

			ManagerDao managerDao = (ManagerDao)DaoFactory.getDao("managerDao");
			CustomerDao customerDao = (CustomerDao)DaoFactory.getDao("customerDao");
			while (rs.next()) {

				OrderTrace orderTrace = new OrderTrace();
				orderTrace.setId(rs.getInt(1));
				orderTrace.setOrderId(rs.getString(2));
				orderTrace.setOrderStatus(rs.getString(3));
				orderTrace.setManager(managerDao.getManagerById(rs.getString(4)));
				orderTrace.setCustomer(customerDao.getCustomerById(rs.getString(5)));
				orderTrace.setOperTime(new java.util.Date(rs.getTimestamp(6).getTime()));

				

				orderTraces.add(orderTrace);

			}

		} catch (SQLException e) {
			e.printStackTrace();
			throw new DataAccessException("组合查询图书列表获取失败!");
		} finally {
			DBUtils.getInstance().releaseRes(ct, ps, rs);
		}

		return orderTraces;
	} 

	public void AddOrderTrace(OrderTrace orderTrace){
		Connection ct = DBUtils.getInstance().getConn();
		PreparedStatement ps = null;
		try {
			ps = ct.prepareStatement(ADD_ORDER_TRACE);
			ps.setString(1, orderTrace.getOrderId());
			ps.setString(2, orderTrace.getOrderStatus());
			ps.setString(3, orderTrace.getManager().getLoginId());
			ps.setString(4, orderTrace.getCustomer().getCustomerId());
			ps.setTimestamp(5, new java.sql.Timestamp(orderTrace.getOperTime().getTime()));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DataAccessException("购买图书信息保存失败，请检查数据!");
		} finally {
			DBUtils.getInstance().releaseRes(ct, ps, null);
		}
	
	
	}
	
}
